﻿Imports System.IO
Imports System.Text

Public Class FileConverHelper

    ''' <summary>
    ''' 将file1内容的以文本的形式输出到file2中
    ''' </summary>
    ''' <param name="file1"></param>
    ''' <param name="file2"></param>
    ''' <remarks></remarks>
    Public Shared Sub Encode(file1 As String, file2 As String)

        Dim fi As FileInfo = New FileInfo(file1)

        Dim fs1 As New FileStream(file1, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim buffer(fi.Length) As Byte
        fs1.Read(buffer, 0, CInt(fi.Length))
        fs1.Close()

        Using sw As New StreamWriter(file2)
            For i As Integer = 0 To buffer.Length - 1
                If i <> buffer.Length - 1 Then
                    sw.Write(buffer(i) & ",")
                Else
                    sw.Write(buffer(i))
                End If
            Next
        End Using

    End Sub

    ''' <summary>
    ''' 将file1文本以二进制的形式输出到file2中
    ''' </summary>
    ''' <param name="file1"></param>
    ''' <param name="file2"></param>
    ''' <remarks></remarks>
    Public Shared Sub Decode(file1 As String, file2 As String)

        Dim s() As String
        Using sr As New StreamReader(file1)
            s = sr.ReadToEnd().Split(",")
        End Using

        Dim buffer(s.Length - 1) As Byte
        For i As Integer = 0 To buffer.Length - 1
            buffer(i) = CInt(s(i))
        Next

        Dim fs2 As New FileStream(file2, FileMode.Create, FileAccess.Write, FileShare.None)
        fs2.Write(buffer, 0, buffer.Length)
        fs2.Close()

    End Sub

End Class
